/*
 *  Class name: TeamCriteriaQueryBuilder.java
 *  Version: 1.0
 *  Date: 19.2.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.persistencelayer.querybuilders.entity;

import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.Team;
import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.querybuilders.EntityCriteriaQueryBuilder;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;

/**
 *
 * @author Michal Ďuriš
 */
public class TeamCriteriaQueryBuilder extends EntityCriteriaQueryBuilder<Team> {

    public TeamCriteriaQueryBuilder(Class<Team> entityClass) {
        super(entityClass);
    }

    @Override
    protected Expression<Boolean> getFieldFilterExpression(String propertyName,
                                                           CriteriaBuilder criteriaBuilder,
                                                           Root<Team> entityRoot) {

        Expression<Boolean> filterFieldExpression;

        if (propertyName == null || propertyName.isEmpty()) {
            return null;
        }

        switch (propertyName) {
            case "bannedTeam":
                filterFieldExpression = criteriaBuilder.notEqual(entityRoot.get(
                        "id").as(Long.class), criteriaBuilder.parameter(
                                Long.class, propertyName));
                break;
            default:
                filterFieldExpression = criteriaBuilder.like(
                        criteriaBuilder.lower(entityRoot.get(propertyName).as(
                                        String.class)),
                        criteriaBuilder.parameter(String.class, propertyName));
                break;
        }

        return filterFieldExpression;
    }

    @Override
    protected Expression<?> getFieldOrderExpression(String propertyName,
                                                    Root<Team> entityRoot) {

        if (propertyName == null || propertyName.isEmpty()) {
            return null;
        }

        return entityRoot.get(propertyName);
    }

}
